package com.oocl.hw1.task3.model.impl;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import com.oocl.hw1.task3.model.DBConnection;
import com.oocl.hw1.task3.pojo.Customer;

public class CustomerDBManager implements DBConnection<Customer> {

	private Set<Customer> set;

	public CustomerDBManager() {
		set = new TreeSet<Customer>();
	}

	@Override
	public boolean add(Customer t) {
		if (t == null)
			return false;
		set.add(t);
		return true;
	}

	@Override
	public Iterator<Customer> get(Comparator<Customer> comparator) {
		if (comparator == null)
			return this.set.iterator();
		
		Set<Customer> setCopy = new TreeSet<Customer>(comparator);
		Iterator<Customer> it = this.set.iterator();
		while(it.hasNext()){
			Customer customer = it.next();
			Customer customerCopy = null;
			try {
				customerCopy = (Customer) customer.clone();
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(customerCopy!=null)
				setCopy.add(customerCopy);
		}
		return setCopy.iterator();
	}

}
